home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group99a.txt
/
000227_icon-group-sender _Fri Nov 5 11:00:16 1999.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
3KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id KAA22015
for icon-group-addresses; Fri, 5 Nov 1999 10:58:32 -0700 (MST)
Message-Id: <199911051758.KAA22015@baskerville.CS.Arizona.EDU>
X-Authentication-Warning: agate.berkeley.edu: news set sender to <news> using -f
From: Steve Wampler <swampler@noao.edu>
X-Newsgroups: comp.lang.icon
Subject: Re: How do I create an array of records??
Date: Fri, 05 Nov 1999 08:17:34 -0700
X-Trace: noao.edu 941815056 33041 140.252.38.6 (5 Nov 1999 15:17:36 GMT)
X-Complaints-To: abuse@noao.edu
X-Accept-Language: en
To: icon-group@optima.CS.Arizona.EDU
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
Andrew McGhie wrote:
>
> I've just started programming with Icon and was wondering how to create
> an array of records, for instance a database of people.
> I've declared a record as follows:
> record Person(surname, forename, age, height)
> but what do I do after this? Or is there a better was to do it?
Hmmm, you have lots of ways to go at this point. For example,
if you really just want an array of records, use a list:
db := list() # create the 'array' (db declared global)
procedure addPerson(surname, forename, age, height)
put(db, Person(surname, forename, age, height))
end
However, it's more likely you'll want to have some way to quickly
look up entries in the database, so you might prefer to have
several tables for quickly locating items using different keys:
dbNames := table()
dbAge := table()
dbHeight := table()
(Note for the more Icon literate - this could be written as:
every (dbNames | dbAge | dbHeight) := table()
but not as:
dbNames := dbAge := dbHeight := table()
do you see why?)
procedure addPerson(surname, forename, age, height)
person := Person(surname, forename, age, height)
/(dbNames[surname]) := [] # Use lists to allow
/(dbAge[age]) := [] # for collisions
/(dbHeight[height]) := []
put(dbNames[surname], person)
put(dbAge[age], person)
put(dbHeight[height], person)
end
Of course, if you're writing a "real" application, you would want
something considerably more sophisticated and with some sanity
checks along the way - and probably want to separate the
database 'operations' [store, search, retrieve, etc] from the
actual items being stored, as in:
procedure addPerson(surname, forename, age, height)
person := Person(surname, forename, age, height)
dbStore(person)
end
--
Steve Wampler- SOLIS Project, National Solar Observatory
swampler@noao.edu